package me.keiwu.admin.controller;

import me.keiwu.admin.domain.PosterTemplate;
import me.keiwu.admin.service.PosterService;
import me.keiwu.pub.data.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * Created by kei on 02/11/2017.
 */
@Controller
@RequestMapping("/api/poster")
public class PosterController {



    @Autowired
    private PosterService posterService;



    @RequestMapping("/template/list")
    public String templateList(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer size, Model model) {
        Page<PosterTemplate> templates = posterService.findByPageAndSize(page, size);
        model.addAttribute("templates", templates);
        return "template";
    }

    @RequestMapping("/template/detail")
    public Response templateDetail(Long id) {
        return Response.ok(posterService.findById(id));
    }


    @RequestMapping("/template/add")
    @ResponseBody
    public Response addTemplate(String title, String desc, String imgSrc, String editData) {
        posterService.addTemplate(title, desc, imgSrc, editData);
        return Response.ok();
    }

    @RequestMapping("/template/update")
    @ResponseBody
    public Response updateTemplate(Long id, String title, String desc, String imgSrc, String editData) {
        posterService.updateTemplate(id, title, desc, imgSrc, editData);
        return Response.ok();
    }

//    @PostMapping("/picture/upload")
//    @ResponseBody
//    public Response uploadPicture(MultipartFile image) {
//        String fileName = MessageFormat.format(
//                "{0}-{1}.jpg",
//                DateUtil.formatDate(new Date(), "yyyyMMddHHmmss", Locale.ENGLISH),
//                RandomStringUtils.randomAlphanumeric(6)
//        );
//        if (FileUtil.uploadFile(image, uploadImgDir, fileName, 700, 700)) {
//            return Response.ok(imgUrlPrefix + fileName);
//        }
//        return Response.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "上传失败");
//    }



}
